package com.lanou.pianke.user.web.userService;

import com.lanou.pianke.article.mapper.ArticleMapper;
import com.lanou.pianke.piece.mapper.PieceMapper;
import com.lanou.pianke.piece.web.entity.PieceIsLike;
import com.lanou.pianke.user.mapper.UserMapper;
import com.lanou.pianke.user.web.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PieceMapper pieceMapper;
    @Autowired
    private ArticleMapper articleMapper;

    public UserNumber selectUserNumberByUid(Long uid) {
        return userMapper.selectByUid(uid);
    }

    public User selectUserByUid(Long uid) {
        return userMapper.selectUdescByUid(uid);
    }

    public List<Piece> selectPieceByUid(Long uid) {
        return userMapper.selectPieceByUid(uid);
    }

    public List<Articles> selectAticlesByUid(Long uid) {
        return userMapper.selectAticlesByUid(uid);
    }

    public Integer updateUserAccount(User user) {
        return userMapper.updateUserAccount(user);
    }

    public void updateLikeByUid(Long uid) {
        List<Piece> pieces = userMapper.selectPieceIdByUid(uid);
        for (Piece piece : pieces) {
            String pieceid = piece.getPieceid();
            List<PieceIsLike> likeList = userMapper.selectIsLikeByPieceId(pieceid);
            for (PieceIsLike isLike : likeList) {
                String pieceid1 = isLike.getPieceid();
                pieceMapper.updatePieceIsLikeStatusByPieceid(pieceid1);
            }
        }
    }

    public Integer selectCommentStatusByPieceId(String pieceid) {
        return userMapper.selectCommentStatusByPieceId(pieceid);
    }

    public Long selectUidByUname(String uname) {

        return userMapper.selectUidByUname(uname);
    }

    public List selectUserByUname(Long uid,String uname) {
        Long uid2 = userMapper.selectUidByUname(uname);
        List<String> list = new ArrayList();
        if (uid2.equals(uid)) {
            List<Piece> pieces = userMapper.selectAllPieceId(uid);
            for (Piece piece : pieces) {
                String pieceid = piece.getPieceid();
                List<Pcomment> pcomments = userMapper.selectCommentByPieceId(pieceid);
                for (Pcomment pcomment : pcomments) {
                    String pcomment_content = pcomment.getPcomment_content();
                    list.add(pcomment_content);
                }
            }
        }
        return list;
    }

    public List<PieceIsLike> selectIsLikeByPieceId(String pieceid) {
        return userMapper.selectIsLikeByPieceId(pieceid);
    }


    public void updatePieceCommentStatusByUid(Long uid) {
        List<Piece> pieces = userMapper.selectPieceIdByUid(uid);
        for (Piece piece : pieces) {
            String pieceid = piece.getPieceid();
            List<Pcomment> pcomments = userMapper.selectPcommentByPieceIdAndStatus(pieceid);
            for (Pcomment pcomment : pcomments) {
                String pcomment_id = pcomment.getPcomment_id();
                userMapper.updatePcommentStatusByPcomment_id(pcomment_id);
            }
        }
    }

    public Integer selectPieceIdByUid(Long uid) {
        List<Piece> list = userMapper.selectPieceIdByUid(uid);
        Integer integer = 0;
        for (Piece piece : list) {
            String pieceid = piece.getPieceid();
            integer = userMapper.selectCommentStatusByPieceId(pieceid);
        }
        return integer;
    }

    public void insertRelation(Long authorUid, Long visitorUid) {
            Integer isreaded = 0;
            UserRelation relation = new UserRelation(authorUid,visitorUid,isreaded);
            userMapper.insertRelation(relation);
    }

    public void updateRelationByUid(Long uid) {
        userMapper.updateRelationByUid(uid);
    }

    public Integer selectPieceLikeByUid(Long uid) {
        return userMapper.selectPieceLikeByUid(uid);
    }

    public Integer selectUserRelationIdByUid(Long uid) {
        return userMapper.selectUserRelationIdByUid(uid);
    }

    public Integer selectUserRelationByUid(Long uid) {
        return pieceMapper.selectUserRelationByUid(uid);
    }

    public Integer selectUserRelationFollowByUid(Long uid) {
        return pieceMapper.selectUserRelationFollowByUid(uid);
    }

    public Integer selectVisitorByUid(Long uid) {
        return pieceMapper.selectVisitorByUid(uid);
    }

    public List<User> selectFansByUid(Long uid) {
        List<Long> longs = userMapper.selectUserRelationByUid(uid);
        List<User> list = new ArrayList<>();
        for (Long aLong : longs) {
            User user = userMapper.selectUserByUid(aLong);
            list.add(user);
        }
        return list;
    }

    public List<User> selectFollowByUid(Long uid) {
        List<Long> longs = userMapper.selectUserRelationFollowByUid(uid);
        List<User> list = new ArrayList<>();
        for (Long aLong : longs) {
            User user = userMapper.selectUserByUid(aLong);
            list.add(user);
        }
        return list;
    }

    public List<User> selectAllVisitorByUid(Long uid) {
        List<Long> longs = userMapper.selectUserVisitorByUid(uid);
        List<User> list = new ArrayList<>();
        for (Long aLong : longs) {
            User user = userMapper.selectUserByUid(aLong);
            list.add(user);
        }
        return list;
    }
}
